module Main where

import Control.Applicative
import System.Environment
import System.Exit
import System.IO.Strict
import System.Process

main :: IO ()
main = do
    args <- getArgs
    bin  <- getEnv "SHELL"
    let cmd = "hmenu_path | hmenu " ++ unwords (map escape args)
    (_, Just h, _, p') <- createProcess $ (shell cmd) { std_out = CreatePipe }
    s  <- init <$> hGetContents h
    ex <- waitForProcess p'
    if ex == ExitSuccess then do
        (_, _, _, p) <- createProcess $ proc bin ["-i", "-c", s]
        exitWith =<< waitForProcess p
      else exitWith ex
  where escape = ("\""++) . (++"\"")

